package com.pshcp.doctor.api;

import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.exception.BizException;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.framework.thread.ReqComTool;
import com.framework.tool.CommonTool;
import com.pshcp.doctor.ao.diagnosissettings.SaveDoctorDiagnosisSettings2DiagnosisFlagAO;
import com.pshcp.doctor.ao.diagnosissettings.SaveDoctorDiagnosisSettingsAO;
import com.pshcp.doctor.tool.DoctorDiagnosisSettingsTool;
import com.pshcp.doctor.vo.diagnosissettings.DoctorDiagnosisSettingsVO;
import com.pshcp.domain.model.DoctorDiagnosisSettings;
import com.pshcp.service.DoctorDiagnosisSettingsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Author: xiaoche
 * @Date: 2022/11/3 20:01
 */
@Api(tags = {"医生端-诊断设置接口"})
@Slf4j
@RestController
@RequestMapping("/api/doctor/diagnosisSettings")
public class DoctorDiagnosisSettingsAPI {

    @Autowired
    private DoctorDiagnosisSettingsService doctorDiagnosisSettingsService;

    @ApiOperation("获取医生诊断设置")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取医生诊断设置")
    @PostMapping("getDoctorDiagnosisSettings")
    public ResultDTO<DoctorDiagnosisSettingsVO> getDoctorDiagnosisSettings() throws BizException {
        DoctorDiagnosisSettings doctorDiagnosisSettings = doctorDiagnosisSettingsService.getDoctorDiagnosisSettingsByDoctorId(ReqComTool.getUserId());
        if (doctorDiagnosisSettings == null) {
            DoctorDiagnosisSettings defaultDoctorDiagnosisSettings = DoctorDiagnosisSettingsTool.buildDefaultDoctorDiagnosisSettings();
            doctorDiagnosisSettingsService.insert(defaultDoctorDiagnosisSettings);
            DoctorDiagnosisSettingsVO defaultDoctorDiagnosisSettingsVO = CommonTool.copyProperties(defaultDoctorDiagnosisSettings, DoctorDiagnosisSettingsVO.class);
            return ResultBuilder.success(defaultDoctorDiagnosisSettingsVO);
        }
        DoctorDiagnosisSettingsVO doctorDiagnosisSettingsVO = CommonTool.copyProperties(doctorDiagnosisSettings, DoctorDiagnosisSettingsVO.class);
        return ResultBuilder.success(doctorDiagnosisSettingsVO);
    }

    @ApiOperation("保存医生诊断设置")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "保存医生诊断设置")
    @PostMapping("saveDoctorDiagnosisSettings")
    public ResultDTO<Long> saveDoctorDiagnosisSettings(@RequestBody SaveDoctorDiagnosisSettingsAO saveDoctorDiagnosisSettingsAO) throws BizException {
        DoctorDiagnosisSettingsTool.checkSaveDoctorDiagnosisSettings(saveDoctorDiagnosisSettingsAO);
        DoctorDiagnosisSettings doctorDiagnosisSettings = doctorDiagnosisSettingsService.getDoctorDiagnosisSettingsByDoctorId(ReqComTool.getUserId());
        if (doctorDiagnosisSettings == null) {
            DoctorDiagnosisSettings saveDoctorDiagnosisSettings = DoctorDiagnosisSettingsTool.buildSaveDoctorDiagnosisSettings(saveDoctorDiagnosisSettingsAO);
            doctorDiagnosisSettingsService.insert(saveDoctorDiagnosisSettings);
        } else {
            DoctorDiagnosisSettings updateDoctorDiagnosisSettings = DoctorDiagnosisSettingsTool.buildSaveDoctorDiagnosisSettings(doctorDiagnosisSettings, saveDoctorDiagnosisSettingsAO);
            doctorDiagnosisSettingsService.update(updateDoctorDiagnosisSettings);
        }
        return ResultBuilder.success(null);
    }

    @ApiOperation("保存医生诊断开关设置")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "保存医生诊断开关设置")
    @PostMapping("saveDoctorDiagnosisSettings2DiagnosisFlag")
    public ResultDTO<Long> saveDoctorDiagnosisSettings2DiagnosisFlag(@RequestBody SaveDoctorDiagnosisSettings2DiagnosisFlagAO saveDoctorDiagnosisSettings2DiagnosisFlagAO) throws BizException {
        CommonTool.checkObj(saveDoctorDiagnosisSettings2DiagnosisFlagAO.getDiagnosisFlag(), "10060002");
        DoctorDiagnosisSettings doctorDiagnosisSettings = doctorDiagnosisSettingsService.getDoctorDiagnosisSettingsByDoctorId(ReqComTool.getUserId());
        if (doctorDiagnosisSettings == null) {
            DoctorDiagnosisSettings saveDoctorDiagnosisSettings = DoctorDiagnosisSettingsTool.buildSaveDoctorDiagnosisSettings2DiagnosisFlag(saveDoctorDiagnosisSettings2DiagnosisFlagAO);
            doctorDiagnosisSettingsService.insert(saveDoctorDiagnosisSettings);
        } else {
            DoctorDiagnosisSettings updateDoctorDiagnosisSettings = DoctorDiagnosisSettingsTool.buildSaveDoctorDiagnosisSettings2DiagnosisFlag(doctorDiagnosisSettings, saveDoctorDiagnosisSettings2DiagnosisFlagAO);
            doctorDiagnosisSettingsService.update(updateDoctorDiagnosisSettings);
        }
        return ResultBuilder.success(null);
    }
}
